<?php
include("./Core/Common.php");
if(count($_GET))$queryArr=$_GET;else$queryArr=$_POST;
@header('Content-Type: application/json; charset=UTF-8');

//删除重复的已登陆微信
$rs=$DB->query("SELECT * FROM `pay_login_qr` WHERE `cookie`='Login_Yes' order by rand() limit 50");
while($res = $rs->fetch())
{
	$nums=$DB->query("SELECT count(*) from `pay_login_qr` where `wx_name`='{$res['wx_name']}'")->fetchColumn();
	if($nums>1){
		$DB->exec("DELETE FROM `pay_login_qr` WHERE `id`='{$res['id']}' limit 1");
	}
}

//删除超时的登陆二维码 200秒后删除
$is_date = date( "Y-m-d H:i:s",(time()-(60*6)));
$DB->exec("DELETE FROM `pay_login_qr` WHERE `addtime`<'{$is_date}' and `cookie`='0' and `type`!='wxpay'");
$DB->exec("DELETE FROM `pay_login_qr` WHERE `addtime`<'{$is_date}' and `cookie`='' and `type`!='wxpay'");
$DB->exec("DELETE FROM `pay_login_qr` WHERE `addtime`<'{$is_date}' and `type`!='wxpay'");
$is_date = date( "Y-m-d H:i:s",(time()-1000));
$DB->exec("DELETE FROM `pay_login_qr` WHERE `addtime`<'{$is_date}' and `cookie`!='Login_Yes' and `type`='wxpay'");
$DB->exec("DELETE FROM `pay_login_qr` WHERE `cookie`='Login_Out' and `type`='wxpay'");


switch(daddslashes($queryArr['act'])){
//####################################   提交获取登录二维码请求   ################################//
case 'Get_Login_QrCode':
	$up_zt = 0;
	$pay_user = 0;
	$pay_pass = 0;
	$type 	 = 	daddslashes($queryArr['type']);		//类型
	$get_url = 	daddslashes($queryArr['get_url']);	//提交过来的码支付域名
	$pay_user = daddslashes($queryArr['pay_user']);	//支付账号
	$pay_pass = daddslashes($queryArr['pay_pass']);	//支付密码
	$get_url=$get_url?$get_url:'INTLPAY';
	$type=$type?$type:'alipay';
	if(!pay_check_ip()){
		$result=array("code"=>-1,"msg"=>"平台验证失败,请购买正版!");
	}else{
		$create_status = 1;
		if($type=='alipay'){
			$INTL_LOGIN_QRCODE = new INTL_LOGIN_QRCODE();//取登陆码协议协议
			$data 	= $INTL_LOGIN_QRCODE->Get_ALILogin_Qr();//获取支付宝登陆码
		}elseif($type=='qqpay'){
			$INTL_LOGIN_QRCODE = new INTL_LOGIN_QRCODE();//取登陆码协议协议
			$data	 = $INTL_LOGIN_QRCODE->Get_QQLogin_Qr();//获取财付通登陆码
		}
		$id = '1'.rand(100000000000000,999999999999999);
		$qr_id 	= $data['id'];
		$msg 	= '成功获取登录二维码';
		$qr_url = $data['qrdata'];
		if($pay_user and $pay_pass){
			$up_zt = 1;
			$Coheck_row = pay_check_ip();
			$Instant_row=$DB->query("SELECT * FROM pay_cloud WHERE qq='{$Coheck_row['qq']}' limit 1")->fetch();
			$alipay_up_outtime = $Instant_row['alipay_up_outtime'];//当前支付宝账号密码自动更新插件授权到期时间
			if($alipay_up_outtime < $date){
				exit(json_encode(array("code"=>-1,"msg"=>"当前站点支付宝账号密码登陆插件未授权或到期,请选择扫码登陆哦!")));
			}else{
				exit(json_encode(array("code"=>-1,"msg"=>"支付宝自动更新插件暂时维护,等待新模式更稳定的自动更新")));
			}
		}
		$DB_SQ = "INSERT INTO `pay_login_qr` (`id`,`get_url`,`type`,`qr_id`,`msg`,`qr_url`,`pay_user`,`pay_pass`,`up_zt`,`cookie`,`addtime`,`create_status`) VALUES ('{$id}','{$get_url}','{$type}','{$qr_id}','{$msg}','{$qr_url}','{$pay_user}','{$pay_pass}','{$up_zt}','0','{$date}','{$create_status}')";
		if($DB->query($DB_SQ)){
			$result=array("code"=>1,"msg"=>'获取登录二维码成功',"id"=>$id,"qr_url"=>$qr_url);
		}else{
			$result=array("code"=>-1,"msg"=>"提交登陆二维码请求失败!".'['.$DB->errorInfo()[1].'] '.$DB->errorInfo()[2]);
		}
	}
break;
//####################################   获取登陆二维码   ################################//
case 'Get_Wx_QrUrl':
	$id  = 	daddslashes($queryArr['id']);		//ID
	if(!pay_check_ip()){
		$result=array("code"=>-1,"msg"=>"平台验证失败,请购买正版!");
	}else{
		$row=$DB->query("SELECT * FROM pay_login_qr WHERE id='{$id}' limit 1")->fetch();
		if($row){
			$result=array("code"=>1,"msg"=>"获取数据成功,出现二维码链接后,请您务必二分钟内扫码,否则二维码会失效!","id"=>$row['id'],"qr_url"=>$row['qr_url']);
		}else{
			$result=array("code"=>-1,"msg"=>"ID不存在!");
		}
	}
break;
//####################################   获取COOKIE   ################################//
case 'Get_Login_Cookie':
	$id 	 = 	daddslashes($queryArr['id']);		//ID
	$row=$DB->query("SELECT * FROM pay_login_qr WHERE id='{$id}' limit 1")->fetch();
	if($row['type']=='alipay'){
		$INTL_LOGIN_QRCODE = new INTL_LOGIN_QRCODE();
		$data = $INTL_LOGIN_QRCODE->Check_AliLogin($row['qr_id']);
	}elseif($row['type']=='qqpay'){
		$INTL_LOGIN_QRCODE = new INTL_LOGIN_QRCODE();
		$data = $INTL_LOGIN_QRCODE->Check_QQLogin($row['qr_id']);
	}
	$msg = $data['msg']?$data['msg']:$row['msg'];
	$cookie = $data['cookie']?$data['cookie']:$row['cookie'];
	$DB->exec("update `pay_login_qr` set `msg` ='{$msg}' where id='{$id}'");
	if(!pay_check_ip()){
		$result=array("code"=>-1,"msg"=>"平台验证失败,请购买正版!");
	}elseif($row){
		$result=array("code"=>1,"msg"=>"获取数据成功!","id"=>$row['id'],"msg"=>$msg,"cookie"=>$cookie);
	}else{
		$result=array("code"=>-1,"msg"=>"ID不存在!");
	}
break;
//####################################   添加待回调金额数据   ################################//
case 'Get_Money_Notify':
	$api_type  = 	urldecode(daddslashes($queryArr['api_type']));		//微信名
	$money 	 = 	urldecode(daddslashes($queryArr['money']));		//微信名
	$url 	 = 	urldecode(daddslashes($queryArr['url']));		//微信名
	$api_type  = daddslashes(substr(md5(urlencode($url)),8,16));
	$money = round($money,2);
	if($money<0.00){
		$result=array("code"=>-4,"msg"=>"请输入正确的对比金额");
	}else{
		if($money>=0.01){//判断是否是收入金额
			$Sql = $DB->query("insert into `pay_notify` (`url`,`api_type`,`money`,`pay_msg`,`status`,`nums`,`crontime`,`addtime`) values ('来源微信：".$url."','".$api_type."','".$money."','暂未回调','0','0','0','".$date."')");
			$id=$DB->lastInsertId();
			$row=$DB->query("SELECT * from `pay_notify` WHERE id='{$id}'and status='0' order by rand() limit 1")->fetch();
			$data = Get_Notify($row);//向各大前台发送回调
		}
		if($Sql){
			$result=array("code"=>1,"msg"=>"提交回调请求成功,等待系统响应,云端订单编号:".$id);
		}else{
			$result=array("code"=>-1,"msg"=>"提交失败");
		}
	}
break;
default:
	exit('{"code":-9,"msg":"No Act"}');
break;
}
	if($result)
		exit(json_encode($result));
	else
		exit($data);
?>